//
//  HZL_travelNotesDetailLocationPageTableViewCell.m
//  B_Project
//
//  Created by 胡佳俊 on 15/11/28.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_travelNotesDetailLocationPageTableViewCell.h"
#import "HZL_travelNotesDetailLocationPageCollectionViewCell.h"
@interface HZL_travelNotesDetailLocationPageTableViewCell ()
<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UIImageView *photo;
@property (nonatomic, retain)UILabel *userNameLabel;
@property (nonatomic, retain)UILabel *titleLabel;//文章标题
@property (nonatomic, retain)UILabel *subTitleLabel; //副标题
@property (nonatomic, retain)UICollectionView *collectionView; //装图片
@property (nonatomic, retain)UICollectionViewFlowLayout *layout;
@property (nonatomic, retain)HZL_RecommendModel *modelWaypoints;

@end
@implementation HZL_travelNotesDetailLocationPageTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{
    self.photo = [[UIImageView alloc]init];
    [self.contentView addSubview:self.photo];
   
    
    self.userNameLabel = [[UILabel alloc ]init];
    self.userNameLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.userNameLabel];
   
    
    self.titleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.titleLabel];
    self.titleLabel.textColor = [UIColor colorWithRed:0.36 green:0.75 blue:0.8 alpha:1];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    
    
    self.subTitleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.subTitleLabel];
    self.subTitleLabel.numberOfLines = 5;
    self.subTitleLabel.font = [UIFont systemFontOfSize:14];


    
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    // self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.layout.minimumInteritemSpacing = 10;
    self.layout.minimumLineSpacing = 10;
    
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.layout] ;
    
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.93 alpha:1];
    [self.contentView addSubview:self.collectionView];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[HZL_travelNotesDetailLocationPageCollectionViewCell class] forCellWithReuseIdentifier:@"itemLocation"];
    
    
}

- (void)layoutSubviews
{
    
    
    [super layoutSubviews];
    
   
        self.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    
    
    
    self.photo.frame = CGRectMake(10, 20, WIDTH / 9, WIDTH / 9);
    self.photo.layer.masksToBounds = YES;
    self.photo.layer.cornerRadius = WIDTH / 18;
    self.userNameLabel.frame = CGRectMake(10 + self.photo.frame.size.width + 20, self.photo.frame.origin.y, WIDTH - 10 - self.photo.frame.size.width - 20 - 10 , 10);
    self.titleLabel.frame = CGRectMake(self.userNameLabel.frame.origin.x, self.userNameLabel.frame.origin.y + 10 + 5,self.userNameLabel.frame.size.width , 10);
    
    
    self.layout.itemSize = CGSizeMake( 200, 150);
    
    
    CGFloat hSubtitle = [[self class]heightWithText:self.modelTrips.trip_text fontSize:14 Width:WIDTH - 20];
    self.subTitleLabel.frame = CGRectMake(self.photo.frame.origin.x,self.photo.frame.origin.y +self.photo.frame.size.height + 15 , WIDTH - 20, hSubtitle);
    
    
    if (self.modelTrips.trip_text.length != 0) {
        self.collectionView.frame  = CGRectMake(10, self.subTitleLabel.frame.origin.y + self.subTitleLabel.frame.size.height + 10, WIDTH - 20, 150);
        
    }
    else
    {
        self.collectionView.frame  = CGRectMake(10, self.photo.frame.origin.y + self.photo.frame.size.height + 15, WIDTH - 20, 150);
        
    }
    
    
}

- (void)setModelAllDate:(HZL_RecommendModel *)modelAllDate
{
    
}
- (void)setModelTrip:(HZL_RecommendModel *)modelTrip
{
    if (_modelTrip != modelTrip) {
        _modelTrip = modelTrip;
    }
    self.titleLabel.text = [NSString stringWithFormat:@"%s %@","@",modelTrip.name];
    self.subTitleLabel.text = modelTrip.trip_text;
    
}
- (void)setModelUser:(HZL_RecommendModel *)modelUser
{
    if (_modelUser != modelUser) {
        _modelUser = modelUser;
    }
    [self.photo sd_setImageWithURL:[NSURL URLWithString:modelUser.avatar_l] placeholderImage:nil];
    self.userNameLabel.text = modelUser.name;
    
}


- (void)setModelTrips:(HZL_RecommendModel *)modelTrips
{
    if (_modelTrips != modelTrips) {
        _modelTrips = modelTrips;
    }
    
    //判断副标题是否有内容
    if (self.modelTrips.trip_text.length != 0) {
        //cell重用过后如果label被移除  重新创建一次
        if (self.subTitleLabel == nil) {
            
            self.subTitleLabel = [[UILabel alloc]init];
            [self.contentView addSubview:self.subTitleLabel];
            self.subTitleLabel.numberOfLines = 5;
            self.subTitleLabel.font = [UIFont systemFontOfSize:14];
   
        }
        self.subTitleLabel.text = modelTrips.trip_text;
    }
    else
    {
        //移除父视图
        [self.subTitleLabel removeFromSuperview];
        
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.waypointsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_travelNotesDetailLocationPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemLocation" forIndexPath:indexPath];

    self.modelWaypoints = self.waypointsArr[indexPath.item];
    
    cell.modelWaypoints = self.waypointsArr[indexPath.item];
    
    
    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
